<template>
  <div class="wrap">
      <el-date-picker
        :editable="false"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd"
        v-model="dateValue"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="dateChange">
      </el-date-picker>
      <el-input placeholder="请输入渠道名称" style="width:200px;" v-model="channelName"></el-input>
    <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
    <el-table
      class="table"
      border fit highlight-current-row
      v-loading="loading"
      element-loading-text="加载中"
      element-loading-spinner="el-icon-loading"
      :data="tableData"
      style="width: 100%">
       <el-table-column
          align="center"
          prop="createTime"
          label="日期">
           <template slot-scope="scope">
            <p> {{_mapping.date_format2(scope.row.createTime)}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          prop="newUser"
          label="新用户数量">
      </el-table-column>
       <el-table-column
          align="center"
          label="1日留存数">
         <template slot-scope="scope">
            <p>{{scope.row.oneSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="1日留存率">
         <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.oneSaveNum ===0 ? 0 :(scope.row.oneSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="2日留存数">
        <template slot-scope="scope">
            <p>{{scope.row.twoSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="2日留存率">
        <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.twoSaveNum ===0 ? 0 :(scope.row.twoSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="3日留存数">
         <template slot-scope="scope">
            <p>{{scope.row.threeSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="3日留存率">
         <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.threeSaveNum ===0 ? 0 :(scope.row.threeSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="4日留存数">
         <template slot-scope="scope">
            <p>{{scope.row.fourSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="4日留存率">
         <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.fourSaveNum ===0 ? 0 :(scope.row.fourSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="5日留存数">
         <template slot-scope="scope">
            <p>{{scope.row.fiveSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="5日留存率">
         <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.fiveSaveNum ===0 ? 0 :(scope.row.fiveSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="6日留存数">
         <template slot-scope="scope">
            <p>{{scope.row.sixSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="6日留存率">
         <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.sixSaveNum ===0 ? 0 :(scope.row.sixSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
         <el-table-column
          align="center"
          label="7日留存数">
         <template slot-scope="scope">
            <p>{{scope.row.sevenSaveNum}}</p>
          </template>
      </el-table-column>
      <el-table-column
          align="center"
          label="7日留存率">
         <template slot-scope="scope">
            <p>{{scope.row.newUser===0 ? '-' :scope.row.sevenSaveNum ===0 ? 0 :(scope.row.sevenSaveNum*100/scope.row.newUser).toFixed(2)+'%'}}</p>
          </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.pageNum"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalNum">
    </el-pagination>
  </div>
</template>
<script>
import _personSave from '@/services/personSave.js'
import _mapping from '@/utils/_mapping.js'
export default {
  data(){
    return {
      loading: false,
      dateValue:'',
      tableData: [{
       
      }],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        }
      },
      query: {
        pageSize: 30,
        pageNum: 1
      },
      totalNum: 0,
      dateValue: '',
      channelName:''
    }
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pageSize = val
      this.getPersonSaveList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.query.pageNum = val
      this.getPersonSaveList()
    },
     // 搜索
    searchClick(){
      this.query.pageSize=30,
      this.query.pageNum = 1,
      this.getPersonSaveList()
    },
   dateChange(val){
      console.log('dateValue', this.dateValue)
      this.getPersonSaveList()
    },
    getPersonSaveList(){
      let params = {
         pageNum: this.query.pageNum,
         pageSize: this.query.pageSize,
         channelName:this.channelName,
        startTime: !this.dateValue ? "" : this.dateValue[0],
        endTime: !this.dateValue ? "" : this.dateValue[1],

      }
      _personSave.getPersonSaveList(params)
      .then(res => {
        this.tableData = res.data.list
        this.totalNum = res.data.total
      })
    }
  },
  created(){
     this.dateValue = [ this._mapping.date_format2(new Date()-24*60*60*1000*7),this._mapping.date_format2(new Date())]
    this.getPersonSaveList()
  
  }
}
</script>
<style lang="scss" scoped>
.wrap{
  .table{
    margin-top: 10px;
    margin-bottom: 25px;
  }
}
</style>